package com.southpark.demo.view.pages.welcome
{
	import com.southpark.demo.control.events.config.LoadThemeEvent;
	import com.southpark.demo.control.events.info.AddInfoEvent;
	import com.southpark.demo.control.events.navigation.AddPageEvent;
	import com.southpark.demo.control.events.user.UserLoginEvent;
	import com.southpark.demo.enum.ServerErrorEnum;
	import com.southpark.demo.interfaces.pages.IHomePage;
	import com.southpark.demo.interfaces.pages.IWelcomePage;
	import com.southpark.demo.view.BaseViewController;
	import com.southpark.demo.view.pages.welcome.interfaces.IWelcomePageCtrl;
	import com.southpark.demo.view.pages.welcome.interfaces.IWelcomePageUI;
	import com.southpark.demo.vo.config.LanguageVO;
	import com.southpark.demo.vo.config.ThemeVO;
	import com.southpark.framework.control.commands.CommandFault;
	import com.southpark.framework.control.responders.IResponder;
	import com.southpark.framework.control.responders.ResponderFactory;
	import com.southpark.system.enum.CookieNameEnum;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	
	
	public class WelcomePageCtrl extends BaseViewController implements IWelcomePage,IWelcomePageCtrl
	{		
		
		public function WelcomePageCtrl(view:IWelcomePageUI)
		{
			super();
			this.view = view;
		}
			
		
		/**
		 * IViewController
		 */
		override public function onCreated():void
		{
			initialize();
		}
		
		override public function onShow():void
		{
			
		}
		
		override public function onHide():void
		{
			
		}
		
		/**
		 * IWelcomePage
		 */
		
		
		
		
		/**
		 * IWelcomePageCtrl
		 */
		private var view:IWelcomePageUI;
		
		private function initialize():void
		{
			var userNameCookie:String = applicationManager.cookieManager.getCookie(CookieNameEnum.USER_NAME);
        	var passwordCookie:String = applicationManager.cookieManager.getCookie(CookieNameEnum.USER_PASSWORD);

        	if (userNameCookie && passwordCookie)
    		{
    			userName = userNameCookie;
    			password = passwordCookie;
    			rememberUser = true;
        	};
        	
        	view.resetFocus();
		} 
		
		/**
		 */
		private var _userName:String;
		
		[Bindable]
		public function set userName(value:String):void {_userName = value};
		public function get userName():String {return _userName};
		
		
		private var _password:String;
		
		[Bindable]
		public function set password(value:String):void {_password = value};
		public function get password():String {return _password};
		
		
		private var _rememberUser:Boolean;
		
		[Bindable]
		public function set rememberUser(value:Boolean):void {_rememberUser = value};
		public function get rememberUser():Boolean {return _rememberUser};
		

		/**
		 */
		public function cancel():void
		{
			applicationManager.infoManager.hidePreviousUserErrors();
			
			var e:AddPageEvent = new AddPageEvent(AddPageEvent.ID);
			e.pageInterface = IHomePage;
			e.dispatch();
		}
		
		
		public function userLogin(userName:String,password:String,rememberUser:Boolean):void
		{
			var responder:IResponder = ResponderFactory.createResponder(onUserLoginSuccess,onUserLoginFault);
			var e:UserLoginEvent = new UserLoginEvent(UserLoginEvent.ID,responder);
			e.userName = userName;
			e.password = password;
			e.rememberUser = rememberUser;
		
			e.dispatch();
		}
		
		private function onUserLoginSuccess(result:Object):void
		{
			var e1:AddInfoEvent = new AddInfoEvent(AddInfoEvent.ADD_CONFIRMATION);
			e1.text = applicationManager.textManager.getText("LOG_009");
			e1.dispatch();
			
			var e2:AddPageEvent = new AddPageEvent(AddPageEvent.ID);
			e2.pageInterface = IHomePage;
			e2.dispatch();
		}
		
		private function onUserLoginFault(commandFault:CommandFault):Boolean
		{
			if (commandFault.faultCode == ServerErrorEnum.LOGIN_ERROR.itsString)
			{
				var e:AddInfoEvent = new AddInfoEvent(AddInfoEvent.ADD_USER_ERROR);
				e.text = applicationManager.textManager.getText("LOG_008");
				e.dispatch();
				
				return true;
			};
			
			return false;
		}
		
		
		/**
		 */
		[Bindable]
		public function set languages(value:IList):void
		{
		}
		public function get languages():IList
		{
			var array:Array = new Array();
			
			for each (var language:LanguageVO in model.configSettings.languages)
			{
				array.push({label:applicationManager.textManager.getText(language.id),value:language.locale});
			};
			
			return (array.length == 0) ? null : new ArrayCollection(array) as IList;
		}
		
		
		public function setLanguage(locale:String):void
		{
			applicationManager.textManager.setLanguage(locale);
		}
		
		
		/**
		 */
		[Bindable]
		public function set themes(value:IList):void
		{
		}
		public function get themes():IList
		{
			var array:Array = new Array();
			
			for each (var theme:ThemeVO in model.configSettings.themes)
			{
				array.push({label:applicationManager.textManager.getText(theme.id),value:theme.source});
			};
			
			return (array.length == 0) ? null : new ArrayCollection(array) as IList;
		}
		
		
		public function loadTheme(source:String):void
		{
			var e:LoadThemeEvent = new LoadThemeEvent(LoadThemeEvent.ID);
			e.source = source;
			e.dispatch();
		}
		
		
	}
}